using UnityEngine;
using System.Collections;

public class AlienScript : PlayerScript {
	
	GameObject attackColider;
	
	public bool attacking = false;
	public float attackDuration = 2.0f;
	public float attackTimer = 0.0f;
	public int attackDamage = 33;
	public SkinnedMeshRenderer ma_mesh;
	public float boostTime = 0;
	
	public void StartAttack()
	{	
		//Debug.Log(ma_mesh);
		Debug.Log("Starting Attack");
		attacking = true;
		ma_mesh.materials[0].color = Color.red;
		attackTimer = 0.0f;
	}
	
	public void FinishAttack()
	{
		Debug.Log("Finish Attack");
		attacking = false;
		attackTimer = 0.0f;
		ma_mesh.materials[0].color = Color.green;
	}
	
	void OnControllerColliderHit(ControllerColliderHit col) 
	{
		//base.OnControllerColliderHit(col);
		//Debug.Log(col.gameObject.name);
		if (col.gameObject.tag != "Untagged")
		{
				PoolManager.Pools["Powerups"].Despawn(col.transform);
			
		switch (col.gameObject.tag) {
			case "bullet":
			{
				BulletScript bullet =  col.gameObject.GetComponent<BulletScript>();
				Health -= bullet.DAMAGE;
				break;
			}
			case "health":
				ItemScript healthScript = col.gameObject.transform.GetComponent<ItemScript>();
				int healthValue = healthScript.healthValue;
				Health += healthValue;
				//Destroy(healthScript.gameObject);
			break;
			case "ammo":
				ItemScript ammoScript = col.gameObject.transform.GetComponent<ItemScript>();
				//Destroy(ammoScript.gameObject);
			break;
			case "speed":
				ItemScript boostScript = col.gameObject.transform.GetComponent<ItemScript>();
				boostTime += boostScript.boostValue;
				//Destroy(boostScript.gameObject);
			break;
		}	
		}
	}
	
	// Use this for initialization
	void Start () {
		Debug.Log ("Alien started");
		ma_mesh = gameObject.GetComponentInChildren<SkinnedMeshRenderer>();
		
		//make arm attack invisible
		//StartAttack();
		movementSpeed = 10;
		boostTime = 0;
		
	}
	
	// Update is called once per frame
	void Update () {
		var myMotor = gameObject.GetComponent<CharacterMotor>();
		//rigidbody.WakeUp();
		if (Health <= 0)
			playerDies();
		if (boostTime > 0) 
		{
			myMotor.movement.maxForwardSpeed = 6;
			boostTime -= Time.deltaTime; 
		}
		else 
		{ 
			myMotor.movement.maxForwardSpeed = 3;
			boostTime = 0;
		}
		
		if (attacking) 
		{ 
			attackTimer += Time.deltaTime;
			Debug.Log(attackTimer);
			if (attackTimer >= attackDuration) 
			{
				Debug.Log(attackTimer);
				attackTimer = 0.0f;
				FinishAttack();
			} 
			
		}
	}
	void OnTriggerStay(Collider _Collider)
	{
	
		if(_Collider.tag == "human")
		{	Debug.Log(_Collider.tag + "Stay");
			
			if(!attacking)
			{
				PlayerScript Player = _Collider.gameObject.GetComponent<HumanScript>();
				if(Player.isAlive)
				{
					Player.Health -= attackDamage;
					StartAttack();
					attacking = true;
				}
				else
				{
					FinishAttack();
				}
			}
		}
	}void OnTriggerEnter(Collider _Collider)
	{
	
		if(_Collider.tag == "human")
		{	Debug.Log(_Collider.tag + "Enter");
			
			if(!attacking)
			{
				StartAttack();
				attacking = true;
			}
		}
	}void OnTriggerExit(Collider _Collider)
	{
	
		if(_Collider.tag == "human")
		{	Debug.Log(_Collider.tag + "Exit");
			
			if(attacking)
			{
				FinishAttack();
				attacking = false;
			}
		}
	}
}
